package com.jingogo.controller;

import com.jingogo.common.Result;
import com.jingogo.entity.CartItem;
import com.jingogo.service.CartService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/cart")
@Tag(name = "购物车", description = "购物车相关接口")
public class CartController {
    @Autowired
    private CartService cartService;

    @PostMapping("/add")
    @Operation(summary = "添加到购物车", description = "添加商品到购物车")
    @PreAuthorize("hasRole('USER')")
    public Result<String> addToCart(@RequestBody CartItem item) {
        cartService.addToCart(item);
        return Result.success("添加成功");
    }

    @PutMapping("/update")
    @Operation(summary = "更新购物车商品数量", description = "更新购物车商品数量")
    @PreAuthorize("hasRole('USER')")
    public Result<String> updateCart(@RequestBody CartItem item) {
        cartService.updateCart(item);
        return Result.success("更新成功");
    }

    @DeleteMapping("/remove")
    @Operation(summary = "移除购物车商品", description = "移除购物车商品")
    @PreAuthorize("hasRole('USER')")
    public Result<String> removeFromCart(@RequestParam Long cartItemId) {
        cartService.removeFromCart(cartItemId);
        return Result.success("移除成功");
    }

    @GetMapping("/list")
    @Operation(summary = "购物车列表", description = "获取用户购物车列表")
    @PreAuthorize("hasRole('USER')")
    public Result<List<CartItem>> listCart(@RequestParam Long userId) {
        return Result.success(cartService.listCart(userId));
    }
} 